package com.vina.web.thymeleaf;


import javax.servlet.http.HttpServletRequest;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

public class TemplateEngineUtil {
	
//	private static String resolverEncoding;
	private static TemplateEngine templateEngine;
	private static TemplateEngineUtil instance;
	
	public static TemplateEngineUtil getInstance(){
		if(instance==null){
			instance = new TemplateEngineUtil();
		}
		initializeTemplateEngine();
		return instance;
	}
	
	private static void initializeTemplateEngine(){
		ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
		templateResolver.setTemplateMode("XHTML");
		templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
        
//        resolverEncoding = templateResolver.getCharacterEncoding();
        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
	}
	
//	public String getEncoding(){
//		return resolverEncoding;
//	}
	public TemplateEngine getEngine(){
		return templateEngine;
	}
	public String getTemplateName(HttpServletRequest request) {
        String requestPath = request.getRequestURI();
        String contextPath = request.getContextPath();
        if (contextPath == null) {
            contextPath = "";
        }

        return requestPath.substring(contextPath.length());
    }
}
